# +_+ coding:utf-8 +_+

import time
import threading
from api import *
import kline_pool
from kline_pool import *
from api.kline import *


__author__ = 'cz'


class MetaSpider(threading.Thread):

    def __init__(self):
        super(MetaSpider, self).__init__()
        pass

    def run(self):
        print "Meta Spider run..."
        #init start add before 2hours ago k line
        timenow = int(time.time())   #seconds
        time2hbefore = timenow-2*60*60
        klines = querybyparam("btc_usd", "quarter", "5min", 30, time2hbefore)
        for item in klines[:-1]:
            print('item:')
            print(item.to_string())
            kline_pool.append(item)
        while True:
            kl = api.kline.query()
            if kl == None :
                time.sleep(1)
                continue
            kline_pool.pool.updatelastone(kl)
            time.sleep(5)